const app = getApp();
const common = require('../../utils/common.js');
const api = require('../../config/api.js');

var pageNo = 1;
var hadLastPage = false;
var isLoad = 0;

Page({
  data: {
    showNoData: false,
    showLoding: true,
    imgUrl: app.globalData.imgurl,
    dataList: [],
    keyword: ''
  },
  onLoad: function (options) {
    var keyword = '';
    if (options.keyword != null && options.keyword != 'undefined' && options.keyword != '') {
      keyword = options.keyword;
    }
    this.setData({
      keyword: keyword
    });
  },
  onShow: function () {
    pageNo = 1;
    hadLastPage = false;
    this.setData({
      dataList: []
    });
    this.loadList();
  },
  loadList: function () {
    isLoad = 1;
    if (hadLastPage) {
      isLoad = 0;
      return;
    }
    var that = this;
    var data = {
      pageNo: pageNo,
      keyword: that.data.keyword
    }
    this.setData({
      showLoding: true,
      showNoData: false
    });
    setTimeout(function () {
      common.request(api.ArticleListData, data, 'POST').then(function (data) {
        that.setData({
          showLoding: false
        });
        if (data.type == 'success') {
          var dataList = that.data.dataList;
          for (var i = 0; i < data.data.dataList.length; i++) {
            dataList.push(data.data.dataList[i]);
          }
          if (data.data.pageNo == data.data.totalPages) {
            hadLastPage = true;
          } else {
            pageNo++;
          }
          that.setData({
            dataList: dataList
          });
          isLoad = 0;
          if (data.data.total == 0) {
            that.setData({
              showNoData: true
            });
          }
        } else {
          console.log(data.content);
          isLoad = 0;
        }
      });
    }, app.globalData.showLodingTime);
  },
  onReachBottom: function () {
    this.loadList();
  },
  toDetail: function (e) {
    var articleId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../newsDetail/newsDetail?articleId=' + articleId
    })
  }
})